package com.loiot.baqi.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.loiot.baqi.controller.response.Pager;
import com.loiot.baqi.dao.BaseDao;
import com.loiot.baqi.dao.TgQqGroupDao;
import com.loiot.baqi.dao.WeiAccountQqGroupBusinessDao;
import com.loiot.baqi.dao.WeiAccountQqGroupDao;
import com.loiot.baqi.service.WeiAccountQqGroupBusinessService;
import com.loiot.baqi.pojo.TgQqGroup;
import com.loiot.baqi.pojo.WeiAccountQqGroup;
import com.loiot.baqi.pojo.WeiAccountQqGroupBusiness;

/**
 * 商务谈判 逻辑类。
 * 
 * @author wangzx
 * @creation 2016-08-11
 */
@Service("weiAccountQqGroupBusinessService")
@Transactional
public class WeiAccountQqGroupBusinessService {

	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Resource
	private WeiAccountQqGroupBusinessDao weiAccountQqGroupBusinessDao;
	@Resource
	private TgQqGroupDao tgQqGroupDao;
	@Resource
	private WeiAccountQqGroupDao weiAccoutQgGroupDao;

	@Resource
	private BaseDao baseDao;

	/**
	 * 查询 商务谈判列表分页
	 * 
	 * @param name
	 *            商务谈判名称
	 * @param pageIndex
	 *            页索引
	 * @return
	 */
	public Pager<WeiAccountQqGroupBusiness> queryWeiAccountQqGroupBusinessListPage(HashMap<String, Object> pMap,
			int pageIndex) throws Exception {

		// 查询商务谈判列表总条数
		int totalResults = weiAccountQqGroupBusinessDao.getWeiAccountQqGroupBusinessListCount(pMap);

		// 构造一个分页器
		Pager<WeiAccountQqGroupBusiness> pager = new Pager<WeiAccountQqGroupBusiness>(totalResults, pageIndex);
		//pager.setPageSize(1);
		// 查询商务谈判列表
		List<WeiAccountQqGroupBusiness> weiAccountQqGroupBusinessList = weiAccountQqGroupBusinessDao
				.queryWeiAccountQqGroupBusinessList(pMap, pager.getSkipResults(), pager.getMaxResults());
		pager.setData(weiAccountQqGroupBusinessList);
		return pager;
	}

	/**
	 * 查询 商务谈判列表分页
	 * 
	 * @param name
	 *            商务谈判名称
	 * @param pageIndex
	 *            页索引
	 * @return
	 */
	public Pager<WeiAccountQqGroupBusiness> queryWeiAccountQqGroupBusinessOverviewListPage(HashMap<String, Object> pMap,
			int pageIndex) throws Exception {

		// 查询商务谈判列表总条数
		int totalResults = weiAccountQqGroupBusinessDao.getWeiAccountQqGroupBusinessOverviewListCount(pMap);

		// 构造一个分页器
		Pager<WeiAccountQqGroupBusiness> pager = new Pager<WeiAccountQqGroupBusiness>(totalResults, pageIndex);

		// 查询商务谈判列表
		List<WeiAccountQqGroupBusiness> weiAccountQqGroupBusinessList = weiAccountQqGroupBusinessDao
				.queryWeiAccountQqGroupBusinessOverviewList(pMap, pager.getSkipResults(), pager.getMaxResults());
		pager.setData(weiAccountQqGroupBusinessList);
		return pager;
	}

	/**
	 * 查询 商务谈判（假）分页
	 * 
	 * @param name
	 *            商务谈判名称
	 * @param pageIndex
	 *            页索引
	 * @return
	 */
	public Pager<WeiAccountQqGroupBusiness> queryFlasePageList(HashMap<String, Object> pMap, int pageIndex)
			throws Exception {
		// 假分页
		Pager<WeiAccountQqGroupBusiness> pager = this.setPkList(pMap, pageIndex);
		List<WeiAccountQqGroupBusiness> weiAccountQqGroupBusinessList = weiAccountQqGroupBusinessDao
				.queryWeiAccountQqGroupBusinessList(pMap);
		pager.setData(weiAccountQqGroupBusinessList);
		return pager;
	}

	/**
	 * 设置 假分页id集合到Map中
	 * 
	 * @param pMap
	 * @param pageIndex
	 * @return
	 * @throws Exception
	 */
	public Pager<WeiAccountQqGroupBusiness> setPkList(HashMap<String, Object> pMap, int pageIndex) throws Exception {
		// 查询职位匹配信息列表总条数
		List<WeiAccountQqGroupBusiness> list = weiAccountQqGroupBusinessDao.queryWeiAccountQqGroupBusinessList(pMap);
		// 构造一个分页器
		Pager<WeiAccountQqGroupBusiness> pager = new Pager<WeiAccountQqGroupBusiness>(list.size(), pageIndex, 5, list);
		List<WeiAccountQqGroupBusiness> idsList = pager.getCurrentPageData();
		List<Long> ids = this.getIds(idsList);
		pMap.put("ids", ids);
		return pager;
	}

	/**
	 * 添加 商务谈判
	 * 
	 * @param p
	 *            参数对象
	 */
	public WeiAccountQqGroupBusiness addWeiAccountQqGroupBusiness(WeiAccountQqGroupBusiness p) throws Exception {
		// return weiAccountQqGroupBusinessDao.addWeiAccountQqGroupBusiness(p);
		return (WeiAccountQqGroupBusiness) this.baseDao.addV1(p);
	}

	/**
	 * 修改 商务谈判
	 * 
	 * @param p
	 *            参数对象
	 */
	public void updateWeiAccountQqGroupBusiness(WeiAccountQqGroupBusiness p) throws Exception {
		weiAccountQqGroupBusinessDao.updateWeiAccountQqGroupBusiness(p);
	}

	/**
	 * 修改 商务谈判
	 * 
	 * @param p
	 *            参数对象
	 */
	public void updateWeiAccountQqGroupBusiness(HashMap<String, Object> pMap) throws Exception {
		weiAccountQqGroupBusinessDao.updateWeiAccountQqGroupBusiness(pMap);
	}

	/**
	 * 删除 商务谈判
	 * 
	 * @param id
	 *            主键
	 */
	public void deleteWeiAccountQqGroupBusiness(java.lang.Long id) throws Exception {
		weiAccountQqGroupBusinessDao.deleteWeiAccountQqGroupBusiness(id);
	}

	/**
	 * 删除 商务谈判
	 * 
	 * @param id
	 *            主键
	 */
	public void deleteWeiAccountQqGroupBusiness(WeiAccountQqGroupBusiness p) throws Exception {
		weiAccountQqGroupBusinessDao.deleteWeiAccountQqGroupBusiness(p);
	}

	/**
	 * 获得 商务谈判
	 * 
	 * @param id
	 *            商务谈判Id
	 * 
	 * @return 返回与ID匹配的商务谈判
	 */
	public WeiAccountQqGroupBusiness getWeiAccountQqGroupBusinessById(java.lang.Long id) throws Exception {
		return weiAccountQqGroupBusinessDao.getWeiAccountQqGroupBusinessById(id);
	}

	/**
	 * 获得 商务谈判
	 * 
	 * @param id
	 *            商务谈判Id
	 * 
	 * @return 返回与ID匹配的商务谈判
	 */
	public WeiAccountQqGroupBusiness getWeiAccountQqGroupBusinessById(java.lang.Long id, Long accountId)
			throws Exception {
		return weiAccountQqGroupBusinessDao.getWeiAccountQqGroupBusinessById(id, accountId);
	}

	/**
	 * 获得 商务谈判
	 * 
	 * @param id
	 *            商务谈判Id
	 * 
	 */
	public WeiAccountQqGroupBusiness getWeiAccountQqGroupBusiness(HashMap<String, Object> pMap) throws Exception {
		return (WeiAccountQqGroupBusiness) weiAccountQqGroupBusinessDao.getWeiAccountQqGroupBusiness(pMap);
	}

	/**
	 * 获得 商务谈判
	 * 
	 * @param name
	 *            商务谈判名称
	 * 
	 * @return 返回与NAME匹配的商务谈判
	 */
	public WeiAccountQqGroupBusiness getWeiAccountQqGroupBusinessByName(String name) throws Exception {
		return weiAccountQqGroupBusinessDao.getWeiAccountQqGroupBusinessByName(name);
	}

	/**
	 * 查询 商务谈判列表
	 * 
	 * @return 商务谈判列表
	 */
	public List<WeiAccountQqGroupBusiness> queryWeiAccountQqGroupBusinessList(HashMap<String, Object> pMap)
			throws Exception {
		return weiAccountQqGroupBusinessDao.queryWeiAccountQqGroupBusinessList(pMap);
	}

	/**
	 * 查询 商务谈判列表
	 * 
	 * @return 商务谈判列表
	 */
	public List<WeiAccountQqGroupBusiness> queryWeiAccountQqGroupBusinessList(WeiAccountQqGroupBusiness p)
			throws Exception {
		return weiAccountQqGroupBusinessDao.queryWeiAccountQqGroupBusinessList(p);
	}

	/**
	 * 查询 商务谈判列表条数
	 * 
	 * @param name
	 *            商务谈判名称
	 * @return 商务谈判列表条数
	 */

	public int getWeiAccountQqGroupBusinessListCount(HashMap<String, Object> pMap) throws Exception {
		return weiAccountQqGroupBusinessDao.getWeiAccountQqGroupBusinessListCount(pMap);
	}

	/**
	 * 查询id集合
	 * 
	 * @return
	 */
	public List<Long> getIds(List<WeiAccountQqGroupBusiness> list) {
		List<Long> idsList = null;
		if (list != null && list.size() > 0) {
			idsList = new ArrayList<Long>();
			for (WeiAccountQqGroupBusiness b : list) {
				idsList.add(null);
			}
		}
		return idsList;
	}

	/**
	 * 统计 商务谈判列表
	 * 
	 * @return 商务谈判列表
	 */
	public List<HashMap<String, Object>> statisticsWeiAccountQqGroupBusinessInfo(HashMap<String, Object> pMap)
			throws Exception {
		return weiAccountQqGroupBusinessDao.statisticsWeiAccountQqGroupBusinessInfo(pMap);
	}
    //通过群id获取群的详细，包括群基本信息、微用户信息，微用户历史信息，商务谈判人信息，商务谈判人历史信息
	//todo 
	public Map<String, Object> getQqGroupAllInfoByGroupId(Long groupId,Integer accountType) throws Exception {
		// TODO Auto-generated method stub
		Map<String,Object> root = new HashMap<String,Object>();
//		TgQqGroup qqGroup = tgQqGroupDao.getTgQqGroupById(groupId);
		HashMap<String,Object> qqGroupPMap = new HashMap<String,Object>();
		qqGroupPMap.put("id", groupId);
		if(accountType.intValue()!=3){//不是管理员
			qqGroupPMap.put("statisticsJlAccountType", accountType);
		}
		TgQqGroup qqGroup = tgQqGroupDao.queryTgQqGroupList(qqGroupPMap).get(0);
		root.put("qqGroup", qqGroup );
		HashMap<String,Object> pMap = new HashMap<String,Object>();
		pMap.put("groupId", qqGroup.getId());
		List<WeiAccountQqGroupBusiness> busiList = weiAccountQqGroupBusinessDao.queryWeiAccountQqGroupBusinessList(pMap); 
		for(WeiAccountQqGroupBusiness bus:busiList){
			if(qqGroup.getQqGroupBusinessId()!=null && qqGroup.getQqGroupBusinessId().equals(bus.getAccountId())){
				root.put("currBusinessPerson", bus);
				break;
			}else{
				root.put("currBusinessPerson", new WeiAccountQqGroupBusiness());
			}
		}
		root.put("busiList", busiList);
		List<WeiAccountQqGroup> weiList = weiAccoutQgGroupDao.queryWeiAccountQqGroupList(pMap);
		for(WeiAccountQqGroup weia:weiList){
			if(qqGroup.getCurrWeiAccountId()!=null && qqGroup.getCurrWeiAccountId().equals(weia.getAccountId())){
			    root.put("currWeiAccount", weia);	
			    break;
			}else{
				root.put("currWeiAccount", new WeiAccountQqGroup());	
			}
		}
		
		root.put("weiList", weiList);
		return root;
	}
}
